package service;

import dao.BookingMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Booking;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class BookingService {
    SqlSession sqlSession = null;
    public List<Booking> list(Integer pageIndex, Integer pageSize, String userName){
        List<Booking>list = new ArrayList<>();
        sqlSession = MyBatisUtil.createSqlSession();
        list = sqlSession.getMapper(BookingMapper.class).list(pageIndex,pageSize,userName);
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }
    public int add(Booking booking){
        int count = 0;
        sqlSession = MyBatisUtil.createSqlSession();
        count = sqlSession.getMapper(BookingMapper.class).add(booking);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }
    public int delete(Integer id){
        int count = 0;
        sqlSession = MyBatisUtil.createSqlSession();
        count = sqlSession.getMapper(BookingMapper.class).delete(id);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }
    public int update(Integer status,Integer id){
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(BookingMapper.class).update(status,id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
    public int cardcount(String cardId){
        int count =0;
        sqlSession = MyBatisUtil.createSqlSession();
        count = sqlSession.getMapper(BookingMapper.class).cardcount(cardId);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }
}


